<?php
// 本类由系统自动生成，仅供测试用途
class BannersAction extends AdminPublicAction {
	
/*======================================================================
	幻燈片列表
======================================================================================*/	
    public function index(){
		
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			 $sort   = I('sort');
			 $upDown = I('updown');
		}else{
			 $_GET['sort'] = $sort = "sort_order";
			 $_GET['updown'] = $upDown = "desc";
			
		}
		import("ORG.Util.Page");
		$page = 10;	
		$Model = M('banner');
		$count = $Model->count();
		$Page = new Page($count, $page);	
		$list   = D('BannersView')->getAll(array('bi.banner_language_id' => (int)L('lan_id')),$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);		
		$page = $Page->show();

		if (S("success") != "")
			{
				$this->assign("msg", '<div class="success">'.S("success").'</div>');
				S('success',NULL);
			}
			
			if (S("err") != "")
			{
				$this->assign("msg", '<div class="err">'.S("err").'</div>');
				S('err',NULL);
			}	
		$this->page = $page;
		$this->listing = $list;	
		$this->left_banner = 'current';
		$this->display();
		
    }
/*======================================================================
	添加幻燈片 &　幻燈片編輯
======================================================================================*/
	public function detail(){
		
		$editId = I('id','',intval);
		$langList = explode(",", C('LANG_LIST'));
		$lang = array();
		foreach($langList as $item)
		{
			$langTxt = '';
			if ($item == 'tchinese') $langTxt = '繁體';
			if ($item == 'schinese') $langTxt = '简体';
			if ($item == 'english')  $langTxt = 'English';
			$lang[] = array("value" => $item, 'txt'=> $langTxt );			
		}		
	
		if(tep_not_null($editId)&&is_numeric($editId)){
			/*編輯*/
			$list   = D('BannersView')->getOne(array('bi.banner_language_id' => (int)L('lan_id'),'b.banner_id' =>$editId));	
			$bannerToLang = D('BannersView')->getAll(array('b.banner_id' =>$editId)); //獲取多語言信息.
			
			foreach ($bannerToLang as $value)
				{				
					$titleArr[$value['banner_language_id']]   =   $value['banner_title'];
					$urlArr[$value['banner_language_id']]   =   $value['banner_url'];
				}		
			
			if ( isset($list["banner_image"]) && $list["banner_image"] != '')
				{
					$imageShow = '<input type="hidden"  name="del_iamge" value="flase" />
								  <div id="image">
								  <a  href="'.__ROOT__.'/Public/images/banners/thumb/m_'.$list["banner_image"].'" class="fancyBox"><img src="'.__ROOT__.'/Public/images/banners/thumb/m_'.$list["banner_image"].'" width="200"></a>
								  <br /><a  href="#" onclick="delImages();return false;"  class="del">Delete</a></div>';
				}
			$this->titleArr = $titleArr;
			$this->urlArr = $urlArr;			  
			$this->listInfo = $list;
			$this->imageShow = $imageShow;	
			$this->editId = $editId;
			$this->addDate = $list['banner_added'];
			$this->action = 'editHandle';
			$this->actionName = 'Save';
		}else{
			/*添加*/
			$this->addDate = date('Y-m-d');
			$this->action = 'addHandle';
			$this->actionName = 'add';
		}
		$this->lang = $lang;
		$this->langList = $langList;
		$this->left_banner = 'current';	
		$this->display();
    }			
		
		
/*======================================================================
	幻燈片添加處理
======================================================================================*/
	public function addHandle(){
			if(IS_POST){
				if(tep_not_null(I('post.banner_added'))){
					$data['banner_added'] = date("Y-m-d",strtotime(I('post.banner_added')));
				}else{
					$data['banner_added'] = date("Y-m-d");
				} 
				$data['sort_order'] = I('post.sort_order');
				$data['banner_status'] = I('post.banner_status',1,intval);
				$data['banner_modification'] = date("Y-m-d H:i:s");
				$insertId = M('banner')->add($data);
				
				$language = explode(",", C('LANG_LIST'));
				$i = 0;
		
				foreach( $language as $item)
				{
					$i++;	
					$map = array();
					$map['banner_id']  = $insertId;	
					$map['banner_title'] = I('post.banner_title_'.$i);
					$map['banner_url'] = I('post.banner_url_'.$i);
					$map['banner_language_id'] = $i;
					M("banner_info")->add($map);
						
				}	
				if ($_FILES['banner_image']['error'] == 0)
				{
					//上传图片
					import('@.ORG.UploadFile');
					$upload = new UploadFile('banner_image');
						//设置上传文件大小 
					$upload->maxSize            = 6000000;
						//设置上传文件类型
					$upload->allowExts          = explode(',', 'jpg,gif,png,jpeg');
						//设置附件上传目录
					$upload->savePath 			=  './Public/images/banners/';
						//设置需要生成缩略图，仅对图像文件有效
					$upload->thumb              = true;
						// 縮略圖路徑
					$upload->thumbPath         =  './Public/images/banners/thumb/';
					// 设置引用图片类库包路径
					$upload->imageClassPath     = '@.ORG.Image';
						//设置需要生成缩略图的文件后缀
					$upload->thumbPrefix        = 'm_,s_';  //m_大缩略图，s_小缩略图
						//设置缩略图最大宽度
					$upload->thumbMaxWidth      = '300,100';
						//设置缩略图最大高度
					$upload->thumbMaxHeight     = '300,100';
						//设置上传文件规则
					$upload->saveRule           = 'uniqid';
					
					if(!$upload->upload()) 
					{			
						S('err','image save failed! '.$upload->getErrorMsg());				
						$this->redirect('index');
					}
					else
					{
						$info =  $upload->getUploadFileInfo();
						M("banner")->where(array("banner_id" => $insertId))->setField('banner_image',$info[0]['savename']);	
						S('success','Banenr successfully added!');			
						$this->redirect('index');			
					}	
				}else{
						S('success','Banenr successfully added!');			
						$this->redirect('index');	
				}
			}else{
				$this->redirect('index');	
			}
			
			
		}	

/*======================================================================
	幻燈片編輯處理
======================================================================================*/
	public function editHandle(){
		if(IS_POST){ 
		
			$id = I('post.editId');
			
			$data['sort_order'] = I('post.sort_order');
			$data['banner_modification'] = date("Y-m-d H:i:s");
			$data['banner_added'] = I('post.banner_added');
			$data['banner_status'] = I('post.banner_status',1,intval);
			M("banner")->where(array('banner_id'=>$id))->save($data);
			
			
			$language = explode(",", C('LANG_LIST'));
			$i = 0;
			$map = array();
			foreach( $language as $item)
			{
				$i++;		
				$map['banner_title'] = I('post.banner_title_'.$i);
				$map['banner_url'] = I('post.banner_url_'.$i);
				M("banner_info")->where(array('banner_id'=> $id,'banner_language_id' => $i))->save($map);
			}
			
			
		if ($_FILES['banner_image']['error'] == 0)
			{
				//上传图片
				import('@.ORG.UploadFile');
				$upload = new UploadFile('banner_image');
					//设置上传文件大小 
				$upload->maxSize            = 6000000;
					//设置上传文件类型
				$upload->allowExts          = explode(',', 'jpg,gif,png,jpeg');
					//设置附件上传目录
				$upload->savePath 			=  './Public/images/banners/';
					//设置需要生成缩略图，仅对图像文件有效
				$upload->thumb              = true;
				// 设置引用图片类库包路径
				$upload->imageClassPath     = '@.ORG.Image';
					//设置需要生成缩略图的文件后缀
				$upload->thumbPrefix        = 'm_,s_';  //m_大缩略图，s_小缩略图
					//设置缩略图最大宽度
				$upload->thumbMaxWidth      = '300,100';
					//设置缩略图最大高度
				$upload->thumbMaxHeight     = '300,100';
					// 縮略圖路徑
				$upload->thumbPath         =  './Public/images/banners/thumb/';
					//设置上传文件规则
				$upload->saveRule           = 'uniqid';
				if(!$upload->upload()) 
				{			
					S('err','image save failed! '.$upload->getErrorMsg());
					$this->redirect('index');
				}
				else
				{
					$image = M("banner")->where(array("banner_id" => $id))->getField('banner_image');
					@unlink("./Public/images/banners/".$image);
					@unlink("./Public/images/banners/thumb/s_".$image);
					@unlink("./Public/images/banners/thumb/m_".$image);
					$info =  $upload->getUploadFileInfo();
					M("banner")->where(array("banner_id" => $id))->setField('banner_image',$info[0]['savename']);	
				}
			}
			else
			{
				if (isset($_POST['del_iamge']) && $_POST['del_iamge'] == 'true')
				{
					$image = M("banner")->where(array("banner_id" => $id))->getField('banner_image');
					@unlink("./Public/images/banners/".$image);
					@unlink("./Public/images/banners/thumb/s_".$image);
					@unlink("./Public/images/banners/thumb/m_".$image);
					M("banner")->where(array('banner_id' => $id))->setField('banner_image','');
				}
			}	
			S('success','Banner update successfully!');
			$this->redirect('index');
		}else{
			$this->redirect('index');
		}
	}

/*======================================================================
	刪除幻燈片
======================================================================================*/
		public function delBanner(){
			$id  = I('id');
			if(tep_not_null($id) && is_numeric($id)){	
				$image = M("banner")->where(array("banner_id" => $id))->getField('banner_image');
				@unlink("./Public/images/banners/".$image);
				@unlink("./Public/images/banners/thumb/s_".$image);
				@unlink("./Public/images/banners/thumb/m_".$image);
				M('banner')->where(array('banner_id' => $id))->delete();  
				M('banner_info')->where(array('banner_id' => $id))->delete();  
				S('success','Banner deleted successfully!');
				$this->redirect('index');
			}else{
				S('err','Banner deleted failure!');
				$this->redirect('index');
			}
		}
/*======================================================================
	幻燈片修改顯示
======================================================================================*/
		public function changeStatus(){			

			$check = M("banner")->where(array("banner_id" => (int)$_GET['id']))->getField('banner_status');	
			$banner_status = ($check == 1)?0:1;		
			M("banner")->where(array("banner_id" => (int)$_GET['id']))->setField('banner_status',$banner_status);
			echo (int)$banner_status;			
			
	}
}